home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / cvs / sprite / add.c < prev    next >
C/C++ Source or Header  |  1991-09-10  |  7KB  |  255 lines

  1. #ifndef lint
  2. static char rcsid[] = "$Id: add.c,v 1.3 91/09/10 16:12:02 jhh Exp $";
  3. #endif !lint
  4.  
  5. /*
  6.  *    Copyright (c) 1989, Brian Berliner
  7.  *
  8.  *    You may distribute under the terms of the GNU General Public License
  9.  *    as specified in the README file that comes with the CVS 1.0 kit.
  10.  *
  11.  * Add
  12.  *
  13.  *    Adds a file or directory to the RCS source repository.  For a file,
  14.  *    the entry is marked as "needing to be added" in the user's own
  15.  *    CVS.adm directory, and really added to the repository when it is
  16.  *    committed.  For a directory, it is added at the appropriate place
  17.  *    in the source repository and a CVS.adm directory is generated
  18.  *    within the directory.
  19.  *
  20.  *    The -m option is currently the only supported option.  Some may wish
  21.  *    to supply standard "rcs" options here, but I've found that this
  22.  *    causes more trouble than anything else.
  23.  *
  24.  *    The user files or directories must already exist.  For a directory,
  25.  *    it must not already have a CVS.adm file in it.
  26.  *
  27.  *    An "add" on a file that has been "remove"d but not committed will
  28.  *    cause the file to be resurrected.
  29.  */
  30.  
  31. #include <sys/param.h>
  32. #include "cvs.h"
  33.  
  34. add(argc, argv)
  35.     int argc;
  36.     char *argv[];
  37. {
  38.     char tmp[MAXPATHLEN], message[MAXMESGLEN];
  39.     register int i;
  40.     int c, err = 0;
  41.     char *targv[5];
  42.  
  43.     if (argc == 1 || argc == -1)
  44.     add_usage();
  45.     message[0] = '\0';
  46.     optind = 1;
  47.     while ((c = getopt(argc, argv, "m:")) != -1) {
  48.     switch (c) {
  49.     case 'm':
  50.         if (strlen(optarg) >= sizeof(message)) {
  51.         warn(0, "warning: message too long; truncated!");
  52.         (void) strncpy(message, optarg, sizeof(message));
  53.         message[sizeof(message) - 1] = '\0';
  54.         } else
  55.         (void) strcpy(message, optarg);
  56.         break;
  57.     case '?':
  58.     default:
  59.         add_usage();
  60.         break;
  61.     }
  62.     }
  63.     argc -= optind;
  64.     argv += optind;
  65.     Name_Repository();
  66.     for (i = 0; i < argc; i++) {
  67.     (void) strcpy(User, argv[i]);
  68.     if (isdir(User)) {
  69.         err += add_directory(User);
  70.         continue;
  71.     }
  72.     (void) sprintf(tmp, "%s/%s", Repository, User);
  73.     if (isdir(tmp)) {
  74.         warn(0, "%s already is a directory in the repository", User);
  75.         err++;
  76.         continue;
  77.     }
  78.     (void) sprintf(Rcs, "%s/%s%s", Repository, User, RCSEXT);
  79.     Version_TS(Rcs, Tag, User);
  80.     if (VN_User[0] == '\0') {
  81.         /*
  82.          * No entry available, TS_Rcs is invalid
  83.          */
  84.         if (VN_Rcs[0] == '\0') {
  85.         /*
  86.          * There is no RCS file either
  87.          */
  88.         if (TS_User[0] == '\0') {
  89.             /*
  90.              * There is no user file either
  91.              */
  92.             warn(0, "nothing known about %s", User);
  93.             err++;
  94.         } else {
  95.             /*
  96.              * There is a user file, so build the entry for it
  97.              */
  98.             if (Build_Entry(message) != 0)
  99.             err++;
  100.         }
  101.         } else {
  102.         /*
  103.          * There is an RCS file already, so somebody else
  104.          * must've added it
  105.          */
  106.         warn(0, "%s added independently by second party", User);
  107.         err++;
  108.         }
  109.     } else if (VN_User[0] == '0' && VN_User[1] == '\0') {
  110.         /*
  111.          * An entry for a new-born file, TS_Rcs is dummy,
  112.          * but that is inappropriate here
  113.          */
  114.         warn(0, "%s has already been entered", User);
  115.         err++;
  116.     } else if (VN_User[0] == '-') {
  117.         /*
  118.          * An entry for a removed file, TS_Rcs is invalid
  119.          */
  120.         if (TS_User[0] == '\0') {
  121.         /*
  122.          * There is no user file (as it should be)
  123.          */
  124.         if (VN_Rcs[0] == '\0') {
  125.             /*
  126.              * There is no RCS file, so somebody else must've
  127.              * removed it from under us
  128.              */
  129.             warn(0, "cannot resurrect %s; RCS file removed by second party",
  130.              User);
  131.             err++;
  132.         } else {
  133.             /*
  134.              * There is an RCS file, so remove the "-" from the
  135.              * version number and restore the file
  136.              */
  137.             (void) strcpy(tmp, VN_User+1);
  138.             (void) strcpy(VN_User, tmp);
  139.             (void) sprintf(tmp, "Resurrected %s", User);
  140.             Register(User, VN_User, tmp);
  141.             targv[0] = "-f";
  142.             targv[1] = "-r";
  143.             targv[2] = VN_User;
  144.             targv[3] = argv[i];
  145.             if (update(4, targv) == 0) {
  146.             warn(0, "%s, version %s, resurrected", User, VN_User);
  147.             } else {
  148.             warn(0, "could not resurrect %s", User);
  149.             err++;
  150.             }
  151.         }
  152.         } else {
  153.         /*
  154.          * The user file shouldn't be there
  155.          */
  156.         warn(0, "%s should be removed and is still there", User);
  157.         err++;
  158.         }
  159.     } else {
  160.         /*
  161.          * A normal entry, TS_Rcs is valid, so it must already be there
  162.          */
  163.         warn(0, "%s already exists, with version number %s", User, VN_User);
  164.         err++;
  165.     }
  166.     }
  167.     Entries2Files();            /* update CVS.adm/Files file */
  168.     exit(err);
  169. }
  170.  
  171. /*
  172.  * The specified user file is really a directory.  So, let's make sure that
  173.  * it is created in the RCS source repository, and that the user's
  174.  * directory is updated to include a CVS.adm directory.
  175.  *
  176.  * Returns 1 on failure, 0 on success.
  177.  */
  178. static
  179. add_directory(dir)
  180.     char *dir;
  181. {
  182.     char cwd[MAXPATHLEN], rcsdir[MAXPATHLEN];
  183.     char message[MAXPATHLEN+100];
  184.  
  185.     if (index(dir, '/') != NULL) {
  186.     warn(0, "directory %s not added; must be a direct sub-directory", dir);
  187.     return (1);
  188.     }
  189.     if (strcmp(dir, CVSADM) == 0) {
  190.     warn(0, "cannot add a '%s' directory", CVSADM);
  191.     return (1);
  192.     }
  193.     if (getwd(cwd) == NULL) {
  194.     warn(0, "cannot get working directory: %s", cwd);
  195.     return (1);
  196.     }
  197.     if (chdir(dir) < 0) {
  198.     warn(1, "cannot chdir to %s", dir);
  199.     return (1);
  200.     }
  201.     if (isfile(CVSADM)) {
  202.     warn(0, "%s/%s already exists", dir, CVSADM);
  203.     goto out;
  204.     }
  205.     (void) sprintf(rcsdir, "%s/%s", Repository, dir);
  206.     if (isfile(rcsdir) && !isdir(rcsdir)) {
  207.     warn(0, "%s is not a directory; %s not added", rcsdir, dir);
  208.     goto out;
  209.     }
  210.     (void) sprintf(message, "Directory %s added to the repository\n", rcsdir);
  211.     if (!isdir(rcsdir)) {
  212.     int omask;
  213.     FILE *fptty;
  214.     char line[MAXLINELEN];
  215.  
  216. #ifdef sprite
  217.     fptty = open_file(getenv("TTY"), "r");
  218. #else
  219.     fptty = open_file("/dev/tty", "r");
  220. #endif
  221.     printf("Add directory %s to the repository (y/n) [n] ? ", rcsdir);
  222.     (void) fflush(stdout);
  223.     if (fgets(line, sizeof(line), fptty) == NULL ||
  224.         (line[0] != 'y' && line[0] != 'Y')) {
  225.         warn(0, "directory %s not added", rcsdir);
  226.         (void) fclose(fptty);
  227.         goto out;
  228.     }
  229.     (void) fclose(fptty);
  230.     omask = umask(2);
  231.     if (mkdir(rcsdir, 0777) < 0) {
  232.         warn(1, "cannot mkdir %s", rcsdir);
  233.         (void) umask(omask);
  234.         goto out;
  235.     }
  236.     (void) umask(omask);
  237.     (void) strcpy(Llist, " - New directory"); /* for title in message */
  238.     Update_Logfile(rcsdir, message);
  239.     }
  240.     Create_Admin(rcsdir, DFLT_RECORD);
  241.     printf("%s", message);
  242. out:
  243.     if (chdir(cwd) < 0)
  244.     error(1, "cannot chdir to %s", cwd);
  245.     return (0);
  246. }
  247.  
  248. static
  249. add_usage()
  250. {
  251.     (void) fprintf(stderr,
  252.            "%s %s [-m 'message'] files...\n", progname, command);
  253.     exit(1);
  254. }
  255.